package com.shangcheng.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shangcheng.common.OrderStatus;
import com.shangcheng.entity.TOrderDetails;
import com.shangcheng.entity.TOrders;
import com.shangcheng.mapper.TCustomerMapper;
import com.shangcheng.mapper.TOrderDetailsMapper;
import com.shangcheng.mapper.TOrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderServiceImpl {
    @Autowired
    private ShopCartServiceImpl cartService;
    @Autowired
    private TOrdersMapper orderDao;
    @Autowired
    private TOrderDetailsMapper detailsDao;
    @Autowired
    private TCustomerMapper customerDao;
    //保存订单
    // 新建订单
    @Transactional
    public Integer newOrder(TOrders order, List<TOrderDetails> details) {
//        order.setOrderNumber(generateOrderNumber());
        order.setStatus(OrderStatus.WAITING_PAYMENT); // 使用枚举值
        order.setCreateTime(LocalDateTime.now());
        orderDao.insert(order);

        for (TOrderDetails detail : details) {
            detail.setOrderId(order.getId());
            detailsDao.insert(detail);
        }
        return order.getId();
    }

    // 获取所有订单
    public List<TOrders> getOrders() {

        return orderDao.selectList(null);
    }
    //根据顾客id查订单
    public List<TOrders> getOrdersById(int customerId) {
        QueryWrapper<TOrders> orderQuery = new QueryWrapper<>();
        orderQuery.eq("custmer_id", customerId);
        return orderDao.selectList(orderQuery);
    }

    // 假支付操作（待支付→已支付）
    @Transactional
    public void fakePayOrder(Integer orderId) {
        TOrders order = orderDao.selectById(orderId);
        if (order != null && order.getStatus() == OrderStatus.WAITING_PAYMENT) {
            order.setStatus(OrderStatus.PAID);
            order.setPayTime(LocalDateTime.now()); // 记录支付时间
            orderDao.updateById(order);
        }
    }

    // 取消订单操作（待支付→已取消）
    @Transactional
    public void cancelOrder(Integer orderId) {
        TOrders order = orderDao.selectById(orderId);
        if (order != null && order.getStatus() == OrderStatus.WAITING_PAYMENT) {
            order.setStatus(OrderStatus.CANCELLED);
            orderDao.updateById(order);
        }
    }

    // 申请退款操作（已支付→申请退款）
    @Transactional
    public void applyRefund(Integer orderId) {
        TOrders order = orderDao.selectById(orderId);
        if (order != null && order.getStatus() == OrderStatus.PAID) {
            order.setStatus(OrderStatus.REFUND_APPLIED);
            order.setRefundTime(LocalDateTime.now()); // 记录退款申请时间
            orderDao.updateById(order);
        }
    }

    // 取消退款申请（申请退款→已支付）
    @Transactional
    public void cancelRefund(Integer orderId) {
        TOrders order = orderDao.selectById(orderId);
        if (order != null && order.getStatus() == OrderStatus.REFUND_APPLIED) {
            order.setStatus(OrderStatus.PAID);
            order.setRefundTime(null); // 清除退款时间
            orderDao.updateById(order);
        }
    }

    // 确认收货（已发货→已完成）
    @Transactional
    public void confirmReceive(Integer orderId) {
        TOrders order = orderDao.selectById(orderId);
        if (order != null && order.getStatus() == OrderStatus.SHIPPED) {
            order.setStatus(OrderStatus.COMPLETED);
            orderDao.updateById(order);
        }
    }

    // 标记已发货（已支付→已发货）
    @Transactional
    public void markShipped(Integer orderId) {
        TOrders order = orderDao.selectById(orderId);
        if (order != null && order.getStatus() == OrderStatus.PAID) {
            order.setStatus(OrderStatus.SHIPPED);
            orderDao.updateById(order);
        }
    }
    @Transactional
    public void deleteOrder(Integer orderId) {
        TOrders order = orderDao.selectById(orderId);
        if (order == null) {
            throw new RuntimeException("订单不存在");
        }
        // 仅允许删除已完成或已取消的订单
        if (order.getStatus() != OrderStatus.COMPLETED && order.getStatus() != OrderStatus.CANCELLED) {
            throw new RuntimeException("仅支持删除已完成或已取消的订单");
        }
        // 先删除订单详情，再删除订单主表
        detailsDao.deleteByOrderId(orderId);
        orderDao.deleteById(orderId);
    }

    // 带ID搜索的订单查询
    public List<TOrders> getOrdersBySearch(Integer orderId) {
        if (orderId == null) {
            return orderDao.selectList(null); // 如果没有传入ID，则查询所有订单
        }

        // 创建一个QueryWrapper来进行查询
        QueryWrapper<TOrders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", orderId); // 使用字段名进行精确查询ID
        return orderDao.selectList(queryWrapper); // 按ID查询订单
    }
}
